#include "logindialog.h"
#include "ui_logindialog.h"
#include "QDebug"
#include<QRegExp>
#include<QMessageBox>

LoginDialog::LoginDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::LoginDialog)
{
    ui->setupUi(this);
    this->setWindowTitle("注册&登录");
    ui->tabWidget->setCurrentIndex(0);//打开窗口显示登录页面
}

LoginDialog::~LoginDialog()
{
    delete ui;
}

//关闭登录注册页面
void LoginDialog::closeEvent(QCloseEvent *event)
{
    event->ignore();
    Q_EMIT SIG_close();
}

//清空注册信息
void LoginDialog::on_pb_clear_reg_clicked()
{
    ui->le_tel_reg->setText("");
    ui->le_password_reg->setText("");
    ui->le_confirm_reg->setText("");
    ui->le_name_reg->setText("");
}

//提交注册信息
void LoginDialog::on_pb_commit_reg_clicked()
{
    QString strTel = ui->le_tel_reg->text();
    QString strPassword = ui->le_password_reg->text();
    QString strConfirm = ui->le_confirm_reg->text();
    QString strName = ui->le_name_reg->text();

    //非空校验
    QString tmpTel = strTel,tmpPassword = strPassword,tmpName = strName;
    if( tmpTel.remove(" ").isEmpty() || tmpPassword.remove(" ").isEmpty() || tmpName.remove(" ").isEmpty() )
    {
        QMessageBox::about(this,"提示","手机号或密码或昵称不能为空");
        return;
    }
    //1.校验手机号 正则表达式
    QRegExp reg("^1[3-8][0-9]\{6,9\}$");
    if(reg.exactMatch(strTel) == false)
    {
        QMessageBox::about(this,"提示","手机号格式错误");
        return;
    }
    //2.校验密码，长度不能超过20
    if(strPassword.length() > 20)
    {
        QMessageBox::about(this,"提示","密码长度超过20");
        return;
    }
    //3.判断两次输入密码是否一致
    if(strPassword != strConfirm)
    {
        QMessageBox::about(this,"提示","两次密码不一致");
        return;
    }
    //4.昵称长度不能超过10
    if(strName.length() > 10)
    {
        QMessageBox::about(this,"提示","昵称长度超过10");
        return;
    }
    Q_EMIT SIG_registerCommit(strTel,strPassword,strName);
}

//清空登录信息
void LoginDialog::on_pb_clear_log_clicked()
{
    ui->le_tel_log->setText("");
    ui->le_password_log->setText("");
}

//提交登录信息
void LoginDialog::on_pb_commit_log_clicked()
{
    QString strTel = ui->le_tel_log->text();
    QString strPassword = ui->le_password_log->text();

    //非空校验
    QString tmpTel = strTel,tmpPassword = strPassword;
    if( tmpTel.remove(" ").isEmpty() || tmpPassword.remove(" ").isEmpty() )
    {
        QMessageBox::about(this,"提示","手机号或密码不能为空");
        return;
    }
    //1.校验手机号 正则表达式
    QRegExp reg("^1[3-8][0-9]\{6,9\}$");
    if(reg.exactMatch(strTel) == false)
    {
        QMessageBox::about(this,"提示","手机号格式错误");
        return;
    }
    //2.校验密码，长度不能超过20
    if(strPassword.length() > 20)
    {
        QMessageBox::about(this,"提示","密码长度超过20");
        return;
    }
    //

    Q_EMIT SIG_loginCommit(strTel,strPassword);
}
